package listener;

import dao.DepartmentDAO;
import dao.DictDAO;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionListener;
import java.util.Map;

@WebListener()
public class ApplicationListener implements ServletContextListener,
        HttpSessionListener, HttpSessionAttributeListener {

    // Public constructor is required by servlet spec
    public ApplicationListener() {
    }

    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    public void contextInitialized(ServletContextEvent sce) {
        /*
            读取字典表，部门表中的两个数据，存储于全局对象application中
        */
        ServletContext application = sce.getServletContext();

        DictDAO dictDAO = DictDAO.getInstance();
        Map<String,String> jobMap = dictDAO.getJobType();
        application.setAttribute("jobMap", jobMap);

        DepartmentDAO departDAO = DepartmentDAO.getInstance();
        Map<String,String> departMap = departDAO.getDepartment();
        application.setAttribute("departMap", departMap);
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context 
         (the Web application) is undeployed or 
         Application Server shuts down.
      */
    }

}
